var config = require('../../../../utils/config');
var API_URL = config.API_URL;
var util = require('../../../../utils/util');
var name, key, account, typ;
Page({
    data: {
        code_disabled: true,
        code_time: '获取验证码',
        name: '',
        bind_disabled: true
    },
    onLoad: function (options) {
        // 拿到绑定的类型和数据
        console.log(options)
        name = options.name;
        account = options.account;
        typ = options.typ;
        let action = options.action;
        console.log(action)
        var icon, title, btn_name;
        var that = this;
        if (name == '邮箱') {
            my.setNavigationBar({
                title: '绑定邮箱',
            })
            icon = '/images/user/email.png';
            btn_name = '绑定';
        } else {
            if (action == 're') {
                title = '更换手机';
                btn_name = '更换';
            } else {
                title = '绑定手机';
                btn_name = '绑定';
            }
            my.setNavigationBar({
                title: title,
            })
            icon = '/images/user/phone.png';
        }
        that.setData({
            name: name,
            account: account,
            icon: icon,
            typ: typ,
            btn_name: btn_name
        })
    },
    account: function (e) {
        var that = this;
        account = e.detail.value;
        console.log(account)
        if (name == '邮箱') {
            if (!util.checkEmail(account)) {
                that.setData({
                    code_disabled: true,
                    bind_disabled: true
                })
                return
            } else {
                that.setData({
                    code_disabled: false,
                    bind_disabled: false
                })
            }
        } else {
            if (!util.checkMobilePhone(account)) {
                that.setData({
                    code_disabled: true,
                    bind_disabled: true
                })
                return
            } else {
                that.setData({
                    code_disabled: false,
                    bind_disabled: false
                })
            }
        }
    },
    send: function (e) {
        var url;
        var that = this;
        that.setData({
            code_disabled: true
        })
        if (name == '邮箱') {
            url = API_URL.sendEmail;
        } else {
            url = API_URL.sendSms;
        }
        var params = {
            mobile: account,
            email: account,
            type: 'bind'
        }
        util.PRL(url, params)
            .then(res => {
                console.log(res)
                if (res.data.status == 200) {
                    key = res.data.data.key;
                    util.countdown(that, 120);
                } else {
                    that.setData({
                        code_disabled: false
                    })
                    util.showNotice(res.data.msg)
                }
            }).catch(res => {
                that.setData({
                    code_disabled: false
                })
                console.log(res);
            })
        },
        // 绑定
          bind: function (e) {
    var code = e.detail.value.code;
    if (code == '') {
      util.showNotice('请输入验证码');
      return
    }
    var params = {
      account: account,
      type: typ,
      key: key,
      code: code
    }
    util.PRL(API_URL.bindAccount, params)
      .then(res => {
        if (res.data.status == 200) {
          my.showToast({
            content: '绑定成功',
            duration: 800,
          })
          let userInfo = my.getStorageSync({key:'user'}).data;
          if(typ=='mobile'){
            userInfo.phone = account;
          }else{
            userInfo.email = account;
          }
          my.setStorage({
            key: 'user',
            data: userInfo,
          })
          setTimeout(function () {
            my.navigateBack({
              delta: 1
            })
          }, 800)
        } else {
          util.showNotice(res.data.msg)
        }
      }).catch(res => {
        console.log(res)
      })
  }
})